//
//  RGSearchUsersListViewController.m
//  RedGrass
//
//  Created by wukong on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchUsersListViewController.h"
#import "RGSearchUserListView.h"
#import "HttpRequest.h"
#import "RGRecommendVideoModel.h"
#import "RGPersonalPageVC.h"

#define pageSize  10

@interface RGSearchUsersListViewController ()<RGSearchUserListViewDelegate>

@property (strong, nonatomic) RGSearchUserListView *searchUserView;
@property (strong, nonatomic) NSMutableArray *userList;
@property (assign, nonatomic) NSInteger pageNum;
@property (strong, nonatomic) NSString *content;

@end

@implementation RGSearchUsersListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self showBackButton];
    _pageNum = 1;
    self.NavStyleType = RGCustomNavStyleType_default;
    self.customNavBar.hidden = YES;
    [self.customContentView addSubview:self.searchUserView];
    [_searchUserView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

#pragma mark - httpQuest
-(void) searchVedioWithContent:(NSString *)content isLoadMore:(BOOL)isloadMore{
    NSDictionary *dic;
    NSString *token = [RGUserInfoModel getUserInfo].token;
    if(isloadMore){
        _pageNum ++;
        dic = @{@"token":SafeStr(token),
                @"content":SafeStr(content),
                @"type":@(2),
                @"pageSize":@(pageSize),
                @"pageNumber":@(_pageNum)
                };
    }else{
        _pageNum = 1;
        _content = content;
        dic = @{@"token":SafeStr(token),
                @"content":SafeStr(content),
                @"type":@(2),
                @"pageSize":@(pageSize),
                @"pageNumber":@(_pageNum)
                };
    }
    
    AA_WEAK_MAKE(self) weakSelf = self;
    HttpRequest *quest = [HttpRequest buildRequest:@"/api/searchResult"];
    [quest post:dic success:^(id data) {
        //        NSLog(@"rrr");
        NSInteger total = [[data objectForKey:@"total"] integerValue];
        NSArray *list = [data objectForKey:@"list"];
        NSMutableArray *tempArr = [NSMutableArray array];
        NSError *error;
        if([list isKindOfClass:[NSArray class]] && list.count){
            tempArr = [RGVideoUserInfoModel arrayOfModelsFromDictionaries:list error:&error];
        }
        if(isloadMore){
            
            if([weakSelf.userList count]){
                if(tempArr.count){
                    [weakSelf.userList addObjectsFromArray:tempArr];
                }
            }else{
                weakSelf.userList = tempArr;
            }
            [weakSelf.searchUserView endFooterFresh];
        }else{
            weakSelf.userList = tempArr;
        }
        if(list.count < pageSize){
            [weakSelf.searchUserView viewHaveNoMoreData];
        }
        [weakSelf.searchUserView reloadDataWithVedioList:weakSelf.userList];
    } fail:^(NSInteger code, NSString *message) {
        [weakSelf showTextMessageWithMessage:message];
        if(isloadMore){
            [weakSelf.searchUserView endFooterFresh];
        }
    }];
}

//关注或者取消关注
-(void) changeFocusStatusWithIndex:(NSIndexPath *)path{
     RGVideoUserInfoModel *model = _userList[path.row];
    NSString *token = [RGUserInfoModel getUserInfo].token;
    NSInteger focusUserId = model.ID;
    NSDictionary *dic = @{@"id":@(focusUserId),
                          @"token":SafeStr(token)
                          };
    if(!model.meFollowmTa){//我没有关注对方
        [self showLoading:@"正在关注Ta"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userFans/add"];
        [quest post:dic success:^(id data) {
            [weakSelf dismissLoading];
            [weakSelf showTextMessageWithMessage:@"成功关注Ta"];
            model.meFollowmTa = YES;
            [weakSelf.searchUserView reloadChosedCellWithIndexPath:path];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf showTextMessageWithMessage:message];
        }];
    }else{//我关注了对方
        [self showLoading:@"正在取消关注Ta"];
        AA_WEAK_MAKE(self) weakSelf = self;
        HttpRequest *quest = [HttpRequest buildRequest:@"/api/userFans/cancel"];
        [quest post:dic success:^(id data) {
            [weakSelf dismissLoading];
            [weakSelf showTextMessageWithMessage:@"成功取消关注Ta"];
            model.meFollowmTa = NO;
            [weakSelf.searchUserView reloadChosedCellWithIndexPath:path];
        } fail:^(NSInteger code, NSString *message) {
            
            [weakSelf showTextMessageWithMessage:message];
        }];
    }
}

#pragma mark - viewDelegate
-(void)loadMoreDataWithView:(RGSearchUserListView *)view{
    [self searchVedioWithContent:SafeStr(_content) isLoadMore:YES];
}

//选中cell
-(void) cellChosedWithView:(RGSearchUserListView *)view chosedIndexPath:(NSIndexPath *)path{
    if(path.row < _userList.count){
        RGVideoUserInfoModel *model = _userList[path.row];
        RGPersonalPageVC *personalVc = [[RGPersonalPageVC alloc]init];
        personalVc.userId = model.ID;
        personalVc.meFollowmTa = model.meFollowmTa;
        personalVc.nickName = SafeStr(model.nickname);
        AA_WEAK_MAKE(self) weakSelf = self;
//        void (^FoucsChangedBlock)(BOOL) = ^(BOOL isFoucs){
//            RGVideoUserInfoModel *model = weakSelf.userList[path.row];
//            model.meFollowmTa = isFoucs;
//            [weakSelf.searchUserView reloadChosedCellWithIndexPath:path];
//        };
        personalVc.focuStatusChangedBlock = ^(BOOL isFoucs){
            RGVideoUserInfoModel *model = weakSelf.userList[path.row];
            model.meFollowmTa = isFoucs;
            [weakSelf.searchUserView reloadChosedCellWithIndexPath:path];
        };
        [self.navigationController pushViewController:personalVc animated:YES];
    }
   
}

-(void) changeFocusStatusWithView:(RGSearchUserListView *)view chosedIndexPath:(NSIndexPath *)path{
    if(path.row < _userList.count){
       [self changeFocusStatusWithIndex:path];
    }
}

#pragma mark - getter

-(RGSearchUserListView *)searchUserView{
    if(!_searchUserView){
        _searchUserView = [[RGSearchUserListView alloc]init];
        _searchUserView.viewDelegate = self;
    }
    return _searchUserView;
}

@end
